package controller

import (
	"RocFamilySystem/internal/service"
	"RocFamilySystem/models"
	"RocFamilySystem/pkg/constant"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/url"
	"strconv"
	"strings"
)

type HardwareDriverController struct {
	BaseController
	HardwareDriverService *service.HardwareDriverService
}

// 初始化
func init() {
	registerContainer["HardwareDriverController"] = &HardwareDriverController{}
}

// SetService 设置服务
func (controller *HardwareDriverController) SetService(hardwareDriverService *service.HardwareDriverService) {
	controller.HardwareDriverService = hardwareDriverService
}

// RegisterRouter 注册路由
func (controller *HardwareDriverController) RegisterRouter(engine *gin.Engine) {
	prefix := "/hardwareDriver"
	g := engine.Group(prefix)
	prefixPermission := "permission" + strings.ReplaceAll(prefix, "/", ":")
	{
		//新增硬件设备
		g.POST("", controller.AddHardwareDriver)
		registerPermissionContainer("新增硬件设备", prefixPermission+":create", prefix, "POST")
		//删除硬件设备
		g.DELETE("/:id", controller.RemoveHardwareDriver)
		registerPermissionContainer("删除硬件设备", prefixPermission+":delete", prefix, "DELETE")
		g.DELETE("/batchDelete", controller.RemoveHardwareDriverBatch)
		registerPermissionContainer("删除硬件设备", prefixPermission+":batchDelete", prefix, "DELETE")
		//修改硬件设备
		g.PUT("", controller.ModifyHardwareDriver)
		registerPermissionContainer("修改硬件设备", prefixPermission+":update", prefix, "PUT")
		//获取硬件设备列表
		g.POST("/list", controller.QueryHardwareDriver)
		registerPermissionContainer("查询硬件设备列表", prefixPermission+":list", prefix, "POST")
		//通过ID查询硬件设备
		g.GET("/:id", controller.QueryHardwareDriverById)
		registerPermissionContainer("ID查询硬件设备", prefixPermission+":id", prefix, "GET")

		//批量导入模板下载
		g.GET("/templateDownload", controller.HardwareDriverBatchImportTemplateDownload)
		registerPermissionContainer("硬件设备批量导入模板下载", prefixPermission+":templateDownload", prefix, "GET")
		//批量导入
		g.POST("/import", controller.HardwareDriverBatchImport)
		registerPermissionContainer("硬件设备批量导入", prefixPermission+":import", prefix, "POST")
		//批量导出
		g.GET("/export", controller.HardwareDriverBatchExport)
		registerPermissionContainer("硬件设备批量导出", prefixPermission+":export", prefix, "POST")
	}
}

// AddHardwareDriver 添加硬件设备
// @Summary AddHardwareDriver 添加硬件设备
// @Tags 硬件设备
// @Produce json
// @Param hardwareDriver body models.HardwareDriver true "硬件设备"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver [post]
func (controller *HardwareDriverController) AddHardwareDriver(ctx *gin.Context) {
	var hardwareDriver models.HardwareDriver
	err := ctx.ShouldBind(&hardwareDriver)
	loginUserId := controller.GetLoginUserId(ctx)
	hardwareDriver.CreateBy = &loginUserId
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.HardwareDriverService.AddHardwareDriver(&hardwareDriver)
	controller.CustomResult(ctx, 200, "添加硬件设备成功", nil)
}

// RemoveHardwareDriver 删除硬件设备
// @Summary RemoveHardwareDriver  删除硬件设备
// @Tags 硬件设备
// @Produce json
// @Param id path int true "硬件设备id"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/:id [delete]
func (controller *HardwareDriverController) RemoveHardwareDriver(ctx *gin.Context) {
	hardwareDriverId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.HardwareDriverService.RemoveHardwareDriver(hardwareDriverId)
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%d】成功", hardwareDriverId), nil)
}

// ModifyHardwareDriver 修改硬件设备
// @Summary ModifyHardwareDriver 修改硬件设备
// @Tags 硬件设备
// @Produce json
// @Param hardwareDriver body models.HardwareDriver true "硬件设备"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver [put]
func (controller *HardwareDriverController) ModifyHardwareDriver(ctx *gin.Context) {
	var hardwareDriver models.HardwareDriver
	err := ctx.ShouldBind(&hardwareDriver)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.HardwareDriverService.ModifyHardwareDriver(&hardwareDriver)
	controller.CustomResult(ctx, 200, fmt.Sprintf("修改【%d】成功", *hardwareDriver.Id), nil)
}

// QueryHardwareDriver 查询硬件设备列表
// @Summary QueryHardwareDriver 查询硬件设备列表
// @Tags 硬件设备
// @Produce json
// @Param hardwareDriver body models.HardwareDriver true "硬件设备"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/list [post]
func (controller *HardwareDriverController) QueryHardwareDriver(ctx *gin.Context) {
	pageNum, pageSize := controller.GetPageInfo(ctx)
	var hardwareDriver models.HardwareDriver
	err := ctx.ShouldBind(&hardwareDriver)
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.HardwareDriverService.QueryHardwareDriver(&hardwareDriver, pageNum, pageSize)
	controller.SuccessResult(ctx, data)
}

// QueryHardwareDriverById 查询硬件设备通过ID
// @Summary QueryHardwareDriverById  查询硬件设备通过ID
// @Tags 硬件设备
// @Produce json
// @Param id path int true "硬件设备id"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/:id [get]
func (controller *HardwareDriverController) QueryHardwareDriverById(ctx *gin.Context) {
	hardwareDriverId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.HardwareDriverService.QueryHardwareDriverById(hardwareDriverId)
	controller.SuccessResult(ctx, data)
}

// RemoveHardwareDriverBatch 批量删除硬件设备
// @Summary RemoveHardwareDriver  批量删除硬件设备
// @Tags 硬件设备
// @Produce json
// @Param ids[] query []int true "硬件设备id"
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/batchDelete [delete]
func (controller *HardwareDriverController) RemoveHardwareDriverBatch(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	controller.HardwareDriverService.RemoveHardwareDriverBatch(ids)
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%v】成功", ids), nil)
}

// HardwareDriverBatchImportTemplateDownload 批量导入模板下载
// @Summary HardwareDriverBatchImportTemplateDownload 批量导入模板下载
// @Tags 硬件设备
// @Produce json
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/templateDownload [get]
func (controller *HardwareDriverController) HardwareDriverBatchImportTemplateDownload(ctx *gin.Context) {
	excelFile := controller.HardwareDriverService.HardwareDriverTemplateDownload()
	ctx.Writer.Header().Set("Content-Type", "application/octet-stream")
	ctx.Writer.Header().Set("Content-Disposition", "attachment;filename="+url.QueryEscape("硬件设备.xlsx"))
	excelFile.Write(ctx.Writer)
}

// HardwareDriverBatchImport 批量导入
// @Summary HardwareDriverBatchImport 批量导入
// @Tags 硬件设备
// @Produce json
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/import [post]
func (controller *HardwareDriverController) HardwareDriverBatchImport(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		controller.HandleError(err, constant.UPLOAD_FILE_ERROR)
	}
	controller.HardwareDriverService.AddHardwareDriverBatch(file)
	controller.CustomResult(ctx, 200, "批量导入成功", nil)
}

// HardwareDriverBatchExport 批量导出
// @Summary HardwareDriverBatchExport 批量导出
// @Tags 硬件设备
// @Produce json
// @Param ids params []int true 导出ids
// @Success 200 {object} Result "成功"
// @Router /hardwareDriver/export [post]
func (controller *HardwareDriverController) HardwareDriverBatchExport(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	err := ctx.ShouldBind(&ids)
	controller.HandleError(err, constant.PARAMS_ERROR)
	excelFile := controller.HardwareDriverService.HardwareDriverExport(ids)
	ctx.Writer.Header().Set("Content-Type", "application/octet-stream")
	ctx.Writer.Header().Set("Content-Disposition", "attachment;filename="+url.QueryEscape("硬件设备.xlsx"))
	excelFile.Write(ctx.Writer)
}
